###########################################
#  Primary Divisions: How Voters Evaluate Policy and Group Differences in Intra-Party Contests
#   - Forthcoming at The Journal of Politics
#   - Henderson et al 2021
#
###########################################
#  - code by S. Goggin & J. Henderson
########################################################
# This file produces affinity scores/measures for candidates using intersction of covariates and experimental items
#  note: unmatched/unweighted is used in final paper/analysis
########################################################
# inputs :: /data/cces_stacked_unmatched.Rdata
# 			 :: candidate_matrix_scored.csv
# 			 :: voter_matrix.csv

# outputs ::
# => data_matrix_scored.Rdata, combines all survey and experiment items to produce shared indicators,
#  i.e., when both have the same level of some experimental item or analogue

#dirs="~/Dropbox/replication0/"
#dirs should be set here or in runR.R 

rm(list=ls()[which(ls()!='dirs')])

library(ggplot2)
library(stringr)

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}

setwd(dirs)
load("data/cces_stacked_unmatched.Rdata")

candidate_matrix=read.csv("data/candidate_matrix.csv",header=T,stringsAsFactors=F)[,-c(1)]
voter_matrix=read.csv("data/voter_matrix.csv",header=T,stringsAsFactors=F)[,-c(1)]

o.order <- c(
"Gender - Male",
"Gender - Female",
"Race - White",
"Race - Black",
"Race - Hispanic",
"Religion - None",
"Religion - Catholic",
"Religion - Evangelical Protestant",
"Religion - Protestant",
"Occupation - Attorney",
"Occupation - CEO",
"Occupation - City Council Member",
"Occupation - Factory Foreman",
"Occupation - Farmer",
"Occupation - Former US Army Major",
"Occupation - Political Staffer",
"Occupation - Small Business Owner",
"Occupation - State Legislator",
"Occupation - Teacher",
"Personality - Decent",
"Personality - Compassionate",
"Personality - Empathetic",
"Personality - Inspiring",
"Personality - Intelligent",
"Personality - Knowledgeable",
"Personality - Moral",
"Personality - Strong Leader",
"Endorsements - Major area newspapers",
"Endorsements - Business groups",
"Endorsements - Christian groups",
"Endorsements - Civil rights groups",
"Endorsements - Energy groups",
"Endorsements - Environmental groups",
"Endorsements - Gun control groups",
"Endorsements - Gun rights groups",
"Endorsements - Labor unions",
"Endorsements - Reproductive rights groups",
"Endorsements - Tax reform groups",
"Endorsements - Tea Party groups",
"Endorsements - Veterans groups",
"Record - Help my constituents get the benefits they deserve",
"Record - Refuse to compromise my principles even when it means taking on my party",
"Record - Secure appointment to a powerful legislative committee",
"Record - Stand with my party to do what's right",
"Record - Work across the aisle to get things done",
"Issue - Promote expanding free trade agreements",
"Issue - Raise taxes on those making more than $250,000 a year",
"Issue - Cut taxes on income and capital gains for all",
"Issue - Defend the rights of LGBT individuals",
"Issue - Defend traditional marriage and religious beliefs",
"Issue - Expand domestic oil and gas production through drilling",
"Issue - Expand government and unemployment assistance for those in need",
"Issue - Prevent and prosecute abuse of government assistance programs",
"Issue - Protect a woman's right to choose",
"Issue - Protect gun owners' rights to defend themselves and others",
"Issue - Protect jobs and industry from unfair foreign trade",
"Issue - Protect the lives of the unborn",
"Issue - Provide a path to citizenship for undocumented immigrants",
"Issue - Reduce the size of military and number of military bases",
"Issue - Reform policing and stop racial profiling",
"Issue - Regulate CO2 emissions to combat global warming",
"Issue - Strengthen border security to stop illegal immigration",
"Issue - Strengthen gun control through commonsense restrictions",
"Issue - Strengthen our military and national defense",
"Issue - Toughen sentences and penalties for criminals")

#voter data side
voter_matrix$budg_policy1 = gsub(voter_matrix$budg_policy1,pattern='Ranked ',replace='') # Budget Priorities - Cut Defense Spending
voter_matrix$budg_policy1[  grep(voter_matrix$budg_policy1,pattern='Skipped')]=2
voter_matrix$budg_policy1=as.numeric(as.factor(voter_matrix$budg_policy1))

voter_matrix$budg_policy2 = gsub(voter_matrix$budg_policy2,pattern='Ranked ',replace='') # Budget Priorities - Cut Domestic Spending
voter_matrix$budg_policy2[  grep(voter_matrix$budg_policy2,pattern='Skipped')]=2
voter_matrix$budg_policy2=as.numeric(as.factor(voter_matrix$budg_policy2))

voter_matrix$budg_policy3 = gsub(voter_matrix$budg_policy3,pattern='Ranked ',replace='') # Budget Priorities - Raise Taxes
voter_matrix$budg_policy3[  grep(voter_matrix$budg_policy3,pattern='Skipped')]=2
voter_matrix$budg_policy3=as.numeric(as.factor(voter_matrix$budg_policy3))

voter_matrix$taxs_policy1 = 1-as.numeric(voter_matrix$budg_policy3==1)
voter_matrix$taxs_policy2 = as.numeric(voter_matrix$budg_policy3==3)

voter_matrix$need_policy1 = as.numeric(voter_matrix$budg_policy2==1)
voter_matrix$need_policy2 = 1-as.numeric(voter_matrix$budg_policy2==3)

#voter_matrix$dfns_policy1 = 1-as.numeric(voter_matrix$budg_policy1==1)
voter_matrix$dfns_policy1 = as.numeric(voter_matrix$budg_policy1==1)
voter_matrix$dfns_policy2 = as.numeric(voter_matrix$budg_policy1==3)

# verify ideological orientation of items?
#lm(voter_matrix$self_place~voter_matrix$envs_policy1)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$envs_policy2)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$envs_policy3)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$envs_policy4)$coef[2]>0

#lm(voter_matrix$self_place~voter_matrix$crme_policy1)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$crme_policy2)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$crme_policy3)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$crme_policy4)$coef[2]>0

#lm(voter_matrix$self_place~voter_matrix$abrt_policy1)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$abrt_policy2)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$abrt_policy3)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$abrt_policy4)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$abrt_policy5)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$abrt_policy6)$coef[2]>0

#lm(voter_matrix$self_place~voter_matrix$gays_policy1)$coef[2]>0

#lm(voter_matrix$self_place~voter_matrix$immi_policy1)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$immi_policy2)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$immi_policy3)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$immi_policy4)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$immi_policy5)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$immi_policy6)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$immi_policy7)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$immi_policy8)$coef[2]>0

#lm(voter_matrix$self_place~voter_matrix$roll_tpp_act)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$roll_trd_adj)$coef[2]>0

#lm(voter_matrix$self_place~voter_matrix$guns_policy1)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$guns_policy2)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$guns_policy3)$coef[2]>0
#lm(voter_matrix$self_place~voter_matrix$guns_policy4)$coef[2]>0

voter_matrix$libcon_envs=(((voter_matrix$envs_policy1+voter_matrix$envs_policy2+voter_matrix$envs_policy3+voter_matrix$envs_policy4)/4)-.5)/.5
voter_matrix$libcon_crme=(((voter_matrix$crme_policy1+voter_matrix$crme_policy2+voter_matrix$crme_policy3+voter_matrix$crme_policy4)/4)-.5)/.5
voter_matrix$libcon_abrt=(((voter_matrix$abrt_policy1+voter_matrix$abrt_policy2+voter_matrix$abrt_policy3+voter_matrix$abrt_policy4+voter_matrix$abrt_policy5+voter_matrix$abrt_policy6)/6)-.5)/.5
voter_matrix$libcon_gays=(voter_matrix$gays_policy1-.5)/.5
voter_matrix$libcon_immi=(((voter_matrix$immi_policy1+voter_matrix$immi_policy2+voter_matrix$immi_policy3+voter_matrix$immi_policy4+voter_matrix$immi_policy5+voter_matrix$immi_policy6+voter_matrix$immi_policy7+voter_matrix$immi_policy8)/8)-.5)/.5
voter_matrix$libcon_trad = (((voter_matrix$roll_tpp_act + voter_matrix$roll_trd_adj)/2)-.5)/.5
voter_matrix$libcon_guns = (((voter_matrix$guns_policy1+voter_matrix$guns_policy2+voter_matrix$guns_policy3+voter_matrix$guns_policy4)/4)-.5)/.5

#voter_matrix$roll_usa_fre
#voter_matrix$dfns_policy1 +
voter_matrix$libcon_dfns = (((voter_matrix$dfns_policy2  + voter_matrix$roll_usa_fre + voter_matrix$roll_iransct)/3)-.5)/.5
#voter_matrix$libcon_dfns = (((voter_matrix$dfns_policy1 + voter_matrix$dfns_policy2)/2)-.5)/.5
#voter_matrix$libcon_dfns = (((voter_matrix$roll_usa_fre + voter_matrix$roll_iransct)/2)-.5)/.5

voter_matrix$libcon_taxs = (((#voter_matrix$taxs_policy1 + voter_matrix$taxs_policy2 +
	voter_matrix$roll_educrfr + voter_matrix$roll_infrast + voter_matrix$roll_medicar +
	voter_matrix$roll_rpl_aca + voter_matrix$roll_minwage)/5)-.5)/.5

#voter_matrix$libcon_taxs = (((voter_matrix$taxs_policy1 + voter_matrix$taxs_policy2)/2)-.5)/.5

#voter_matrix$libcon_need = (((voter_matrix$need_policy1 + voter_matrix$need_policy2 +
#	voter_matrix$roll_educrfr + voter_matrix$roll_infrast + voter_matrix$roll_medicar +
#	voter_matrix$roll_rpl_aca + voter_matrix$roll_minwage)/7)-.5)/.5

voter_matrix$libcon_need = (((#voter_matrix$need_policy1 + voter_matrix$need_policy2 +
	#voter_matrix$roll_educrfr + voter_matrix$roll_infrast +
  voter_matrix$roll_medicar +
	voter_matrix$roll_rpl_aca + voter_matrix$roll_minwage)/3)-.5)/.5


mu=rowMeans(cbind(
  voter_matrix$libcon_envs,
  voter_matrix$libcon_crme,
  voter_matrix$libcon_abrt,
  voter_matrix$libcon_gays,
  voter_matrix$libcon_immi,
  voter_matrix$libcon_trad,
  voter_matrix$libcon_guns,
  voter_matrix$libcon_dfns,#*
  voter_matrix$libcon_taxs,#*
  voter_matrix$libcon_need))


voter_matrix$ii_male 	= as.numeric(candidate_matrix$g_male == 1   & voter_matrix$male == 1)
voter_matrix$ii_female 	= as.numeric(candidate_matrix$g_female == 1 & voter_matrix$female == 1)

voter_matrix$ii_white 	 = as.numeric(candidate_matrix$re_white == 1    & voter_matrix$white == 1)
voter_matrix$ii_black 	 = as.numeric(candidate_matrix$re_black == 1    & voter_matrix$black == 1)
voter_matrix$ii_hispanic = as.numeric(candidate_matrix$re_hispanic == 1 & voter_matrix$hisps == 1)

voter_matrix$ii_r_none 		 = as.numeric(candidate_matrix$r_none == 1        & voter_matrix$religion_none == 1)
voter_matrix$ii_r_catholic 	 = as.numeric(candidate_matrix$r_catholic == 1    & voter_matrix$religion_catholic == 1)
voter_matrix$ii_r_evangelical= as.numeric(candidate_matrix$r_evangelical == 1 & voter_matrix$religion_evangelical == 1)
voter_matrix$ii_r_protestant = as.numeric(candidate_matrix$r_protestant == 1  & voter_matrix$religion_protestant == 1)

voter_matrix$ii_attorney 		= as.numeric(candidate_matrix$o_attorney == 1   	  & voter_matrix$job_attorney == 1)
voter_matrix$ii_ceo 			= as.numeric(candidate_matrix$o_ceo == 1              & voter_matrix$job_ceo == 1)
voter_matrix$ii_citycouncil 	= as.numeric(candidate_matrix$o_citycouncil == 1      & voter_matrix$job_citycouncil == 1)
voter_matrix$ii_factoryforeman 	= as.numeric(candidate_matrix$o_factoryforeman == 1	  & voter_matrix$job_factory == 1)
voter_matrix$ii_farmer 			= as.numeric(candidate_matrix$o_farmer == 1           & voter_matrix$job_farmer == 1)
voter_matrix$ii_usarmymajor		= as.numeric(candidate_matrix$o_usarmymajor == 1      & voter_matrix$job_military == 1)
voter_matrix$ii_politicalstaffer= as.numeric(candidate_matrix$o_politicalstaffer == 1 & voter_matrix$job_polstaffer == 1)
voter_matrix$ii_smallbizowner 	= as.numeric(candidate_matrix$o_smallbizowner == 1    &	voter_matrix$job_smallbusiness == 1)
voter_matrix$ii_stateleg 		= as.numeric(candidate_matrix$o_stateleg == 1         & voter_matrix$job_citycouncil == 1)
voter_matrix$ii_teacher 		= as.numeric(candidate_matrix$o_teacher == 1          & voter_matrix$job_teacher == 1)


voter_matrix$ii_need	 = as.numeric(candidate_matrix$i_need == 1 & voter_matrix$libcon_need<=0)
voter_matrix$ii_govabuse = as.numeric(candidate_matrix$i_govabuse == 1 & voter_matrix$libcon_need>=0)

voter_matrix$ii_reducemilitary	   = as.numeric(candidate_matrix$i_reducemilitary==1     & voter_matrix$libcon_dfns<=0)
voter_matrix$ii_strengthenmilitary = as.numeric(candidate_matrix$i_strengthenmilitary==1 & voter_matrix$libcon_dfns>=0)

voter_matrix$ii_raisetaxes = as.numeric(candidate_matrix$i_raisetaxes==1 & voter_matrix$libcon_taxs<=0)
voter_matrix$ii_cuttaxes   = as.numeric(candidate_matrix$i_cuttaxes==1   & voter_matrix$libcon_taxs>=0)

voter_matrix$ii_lgbt     = as.numeric(candidate_matrix$i_lgbt==1     & voter_matrix$libcon_gays<=0)
voter_matrix$ii_marriage = as.numeric(candidate_matrix$i_marriage==1 & voter_matrix$libcon_gays>=0)

voter_matrix$ii_co2emissions = as.numeric(candidate_matrix$i_co2emissions==1 & voter_matrix$libcon_envs<=0)
voter_matrix$ii_drilling     = as.numeric(candidate_matrix$i_drilling==1     & voter_matrix$libcon_envs>=0)

voter_matrix$ii_freetrade   = as.numeric(candidate_matrix$i_freetrade==1   & voter_matrix$libcon_trad<=0)
voter_matrix$ii_unfairtrade = as.numeric(candidate_matrix$i_unfairtrade==1 & voter_matrix$libcon_trad>=0)

voter_matrix$ii_righttochoose = as.numeric(candidate_matrix$i_righttochoose==1 & voter_matrix$libcon_abrt<=0)
voter_matrix$ii_unbornlives   = as.numeric(candidate_matrix$i_unbornlives==1   & voter_matrix$libcon_abrt>=0)

voter_matrix$ii_guncontrol = as.numeric(candidate_matrix$i_guncontrol==1 & voter_matrix$libcon_guns<=0)
voter_matrix$ii_gunrights  = as.numeric(candidate_matrix$i_gunrights==1  & voter_matrix$libcon_guns>=0)

voter_matrix$ii_citizenship    = as.numeric(candidate_matrix$i_citizenship==1    & voter_matrix$libcon_immi<=0)
voter_matrix$ii_bordersecurity = as.numeric(candidate_matrix$i_bordersecurity==1 & voter_matrix$libcon_immi>=0)

voter_matrix$ii_policing  = as.numeric(candidate_matrix$i_policing==1  & voter_matrix$libcon_crme<=0)
voter_matrix$ii_criminals = as.numeric(candidate_matrix$i_criminals==1 & voter_matrix$libcon_crme>=0)


# y, y :: - trade (jobs)		| protection 			| business 					| tea party | labor
# y, y :: - lgbt 				| traditional marriage  | christian 				| tea party | labor
# y, y :: - govt assist 		| assist abuse 			| civil rights | business	| tea party | labor
# y, y :: - immigrant citizen 	| border security 		| business 					| tea party | labor
# y, y :: - prochoice 			| prolife  				| reproductive | christian 	| tea party | labor
# y, y :: - taxes hikes 		| tax cuts 				| taxes | business 			| tea party | labor
# y, y :: - co2 env 			| oil energy  			| env | energy              | tea party | labor
# y, y :: - policing/race 		| crime 				| civil rights              | tea party | labor
# y, y :: - defense  			| strong military 		| vets                      | tea party | labor
# y, y :: - gun control  		| gun rights 			| guns control | gun rights | tea party | labor

voter_matrix$ee_teaparty 	= as.numeric(candidate_matrix$e_teaparty == 1 &
	voter_matrix$libcon_envs + #voter_matrix$libcon_crme +
	#voter_matrix$libcon_abrt + voter_matrix$libcon_gays +
	voter_matrix$libcon_immi + voter_matrix$libcon_trad +
	#voter_matrix$libcon_guns + voter_matrix$libcon_dfns +
	voter_matrix$libcon_need + voter_matrix$libcon_taxs >= 0)

voter_matrix$ee_christian = as.numeric(candidate_matrix$e_christian == 1 &
	#voter_matrix$libcon_envs + #voter_matrix$libcon_crme +
	voter_matrix$libcon_abrt +
	voter_matrix$libcon_gays #+
	#voter_matrix$libcon_immi + voter_matrix$libcon_trad +
	#voter_matrix$libcon_guns + voter_matrix$libcon_dfns +
	#voter_matrix$libcon_need + voter_matrix$libcon_taxs
	>=0)

voter_matrix$ee_gunrights = as.numeric(candidate_matrix$e_gunrights == 1 &
	#voter_matrix$libcon_envs + #
	#voter_matrix$libcon_crme +
	#voter_matrix$libcon_abrt + voter_matrix$libcon_gays +
	#voter_matrix$libcon_immi + voter_matrix$libcon_trad +
	#
	voter_matrix$libcon_guns #+ voter_matrix$libcon_dfns +
	#voter_matrix$libcon_need + voter_matrix$libcon_taxs
	>= 0)

voter_matrix$ee_energy	= as.numeric(candidate_matrix$e_energy == 1 &
	#
	voter_matrix$libcon_envs #+ #voter_matrix$libcon_crme +
	#voter_matrix$libcon_abrt + voter_matrix$libcon_gays +
	#voter_matrix$libcon_immi + voter_matrix$libcon_trad +
	#voter_matrix$libcon_guns + voter_matrix$libcon_dfns +
	#voter_matrix$libcon_need + voter_matrix$libcon_taxs
	>= 0)

voter_matrix$ee_taxreform = as.numeric(candidate_matrix$e_taxreform == 1 &
	#voter_matrix$libcon_envs + #voter_matrix$libcon_crme +
	#voter_matrix$libcon_abrt + voter_matrix$libcon_gays +
	#voter_matrix$libcon_immi + voter_matrix$libcon_trad +
	#voter_matrix$libcon_guns + voter_matrix$libcon_dfns +
	#voter_matrix$libcon_need +
	voter_matrix$libcon_taxs >= 0)


voter_matrix$ee_business = as.numeric(candidate_matrix$e_business == 1 &
	#voter_matrix$libcon_envs + #voter_matrix$libcon_crme +
	#voter_matrix$libcon_abrt + voter_matrix$libcon_gays +
	-voter_matrix$libcon_immi +
	voter_matrix$libcon_trad +
	#voter_matrix$libcon_guns + voter_matrix$libcon_dfns +
	voter_matrix$libcon_need + voter_matrix$libcon_taxs >= 0)

voter_matrix$ee_veterans = as.numeric(candidate_matrix$e_veterans == 1 &
	#voter_matrix$libcon_envs + #voter_matrix$libcon_crme +
	#voter_matrix$libcon_abrt + voter_matrix$libcon_gays +
	#voter_matrix$libcon_immi + voter_matrix$libcon_trad +
	#voter_matrix$libcon_guns +
	voter_matrix$libcon_dfns #+
	#voter_matrix$libcon_need #+ voter_matrix$libcon_taxs
	>= 0)

voter_matrix$ee_laborunions = as.numeric(candidate_matrix$e_laborunions == 1 &
	voter_matrix$libcon_envs + #voter_matrix$libcon_crme +
	#voter_matrix$libcon_abrt + voter_matrix$libcon_gays +
	voter_matrix$libcon_immi + voter_matrix$libcon_trad +
	#voter_matrix$libcon_guns + voter_matrix$libcon_dfns +
	voter_matrix$libcon_need + voter_matrix$libcon_taxs <= 0)

voter_matrix$ee_civilrights = as.numeric(candidate_matrix$e_civilrights == 1 &
	#voter_matrix$libcon_envs + #
	voter_matrix$libcon_crme +
	#voter_matrix$libcon_abrt + voter_matrix$libcon_gays +
	#voter_matrix$libcon_immi + voter_matrix$libcon_trad +
	#voter_matrix$libcon_guns + voter_matrix$libcon_dfns +
	voter_matrix$libcon_need #+ voter_matrix$libcon_taxs
	<= 0)

voter_matrix$ee_environment	= as.numeric(candidate_matrix$e_environment == 1 &
	#
	voter_matrix$libcon_envs #+ #voter_matrix$libcon_crme +
	#voter_matrix$libcon_abrt + voter_matrix$libcon_gays +
	#voter_matrix$libcon_immi + voter_matrix$libcon_trad +
	#voter_matrix$libcon_guns + voter_matrix$libcon_dfns +
	#voter_matrix$libcon_need + voter_matrix$libcon_taxs
	<= 0)

voter_matrix$ee_guncontrol = as.numeric(candidate_matrix$e_guncontrol == 1 &
	#voter_matrix$libcon_envs + #
	#voter_matrix$libcon_crme +
	#voter_matrix$libcon_abrt + voter_matrix$libcon_gays +
	#voter_matrix$libcon_immi + voter_matrix$libcon_trad +
	#
	voter_matrix$libcon_guns #+ voter_matrix$libcon_dfns +
	#voter_matrix$libcon_need + voter_matrix$libcon_taxs
	<= 0)

voter_matrix$ee_reproductive = as.numeric(candidate_matrix$e_reproductive == 1 &
	#voter_matrix$libcon_envs + #voter_matrix$libcon_crme +
	#
	voter_matrix$libcon_abrt #+ voter_matrix$libcon_gays +
	#voter_matrix$libcon_immi + voter_matrix$libcon_trad +
	#voter_matrix$libcon_guns + voter_matrix$libcon_dfns +
	#voter_matrix$libcon_need + voter_matrix$libcon_taxs
	<= 0)

# ee_newspapers


voter_matrix$ii_gender   = (voter_matrix$ii_male + voter_matrix$ii_female)
voter_matrix$ii_race     = (voter_matrix$ii_white+voter_matrix$ii_black+voter_matrix$ii_hispanic)
voter_matrix$ii_religion = (voter_matrix$ii_r_none+voter_matrix$ii_r_catholic+voter_matrix$ii_r_evangelical+voter_matrix$ii_r_protestant)
voter_matrix$ii_occupation = (
	voter_matrix$ii_attorney+voter_matrix$ii_ceo+voter_matrix$ii_citycouncil+voter_matrix$ii_factoryforeman+voter_matrix$ii_farmer+
	voter_matrix$ii_usarmymajor+voter_matrix$ii_politicalstaffer+voter_matrix$ii_smallbizowner+voter_matrix$ii_stateleg+voter_matrix$ii_teacher
)
voter_matrix$ii_issues = (
	voter_matrix$ii_criminals         +voter_matrix$ii_policing      +voter_matrix$ii_bordersecurity+voter_matrix$ii_citizenship+
	voter_matrix$ii_gunrights         +voter_matrix$ii_guncontrol    +voter_matrix$ii_unbornlives   +voter_matrix$ii_righttochoose+
	voter_matrix$ii_unfairtrade       +voter_matrix$ii_freetrade     +voter_matrix$ii_drilling      +voter_matrix$ii_co2emissions+
	voter_matrix$ii_marriage          +voter_matrix$ii_lgbt          +voter_matrix$ii_cuttaxes      +voter_matrix$ii_raisetaxes+
	voter_matrix$ii_strengthenmilitary+voter_matrix$ii_reducemilitary+voter_matrix$ii_govabuse      +voter_matrix$ii_need
)

voter_matrix$ii_endorse = (
	voter_matrix$ee_reproductive + voter_matrix$ee_guncontrol  + voter_matrix$ee_environment +
	voter_matrix$ee_civilrights  + voter_matrix$ee_laborunions + voter_matrix$ee_veterans    +
	voter_matrix$ee_business     + voter_matrix$ee_taxreform   + voter_matrix$ee_energy      +
	voter_matrix$ee_gunrights    + voter_matrix$ee_christian   + voter_matrix$ee_teaparty
)

# these are fairly error prone since imputed from 10% of data or so, probably can't use
voter_matrix$imp_citizenship    = as.numeric(candidate_matrix$i_citizenship==1    & voter_matrix$mip_immigration==1)
voter_matrix$imp_bordersecurity = as.numeric(candidate_matrix$i_bordersecurity==1 & voter_matrix$mip_immigration==1)

voter_matrix$imp_guncontrol = as.numeric(candidate_matrix$i_guncontrol==1 & voter_matrix$mip_guns==1)
voter_matrix$imp_gunrights  = as.numeric(candidate_matrix$i_gunrights==1  & voter_matrix$mip_guns==1)

voter_matrix$imp_righttochoose = as.numeric(candidate_matrix$i_righttochoose==1 & voter_matrix$mip_abortion==1)
voter_matrix$imp_unbornlives   = as.numeric(candidate_matrix$i_unbornlives==1   & voter_matrix$mip_abortion==1)

voter_matrix$imp_raisetaxes = as.numeric(candidate_matrix$i_raisetaxes==1 & voter_matrix$mip_taxes==1)
voter_matrix$imp_cuttaxes   = as.numeric(candidate_matrix$i_cuttaxes==1   & voter_matrix$mip_taxes==1)

voter_matrix$imp_co2emissions = as.numeric(candidate_matrix$i_co2emissions==1 & voter_matrix$mip_env==1)
voter_matrix$imp_drilling     = as.numeric(candidate_matrix$i_drilling==1     & voter_matrix$mip_env==1)

voter_matrix$imp_freetrade   = as.numeric(candidate_matrix$i_freetrade==1   & voter_matrix$mip_jobs==1)
voter_matrix$imp_unfairtrade = as.numeric(candidate_matrix$i_unfairtrade==1 & voter_matrix$mip_jobs==1)

voter_matrix$imp_lgbt     = as.numeric(candidate_matrix$i_lgbt==1     & voter_matrix$mip_gays==1)
voter_matrix$imp_marriage = as.numeric(candidate_matrix$i_marriage==1 & voter_matrix$mip_gays==1)

voter_matrix$imp_policing  = as.numeric(candidate_matrix$i_policing==1  & voter_matrix$mip_race==1)
voter_matrix$imp_criminals = as.numeric(candidate_matrix$i_criminals==1 & voter_matrix$mip_crime==1)

voter_matrix$imp_reducemilitary	    = as.numeric(candidate_matrix$i_reducemilitary==1     & (voter_matrix$mip_defense==1 | voter_matrix$mip_natlsecurity==1))#(voter_matrix$mip_socsec==1 | voter_matrix$mip_healthcare==1))
voter_matrix$imp_strengthenmilitary = as.numeric(candidate_matrix$i_strengthenmilitary==1 & (voter_matrix$mip_defense==1 | voter_matrix$mip_natlsecurity==1))

voter_matrix$imp_need	  = as.numeric(candidate_matrix$i_need == 1 & (voter_matrix$mip_deficit==1 | voter_matrix$mip_govtcorruption==1))#(voter_matrix$mip_socsec==1 | voter_matrix$mip_healthcare==1))
voter_matrix$imp_govabuse = as.numeric(candidate_matrix$i_govabuse==1 & (voter_matrix$mip_deficit==1 | voter_matrix$mip_govtcorruption==1))

data_matrix=cbind(candidate_matrix,voter_matrix)
save(data_matrix,file='data/data_matrix_scored.Rdata')

#END 3_build_affinity_score

# trying to keep everything together in the data
# -- cces_stacked in data/cces_stacked_unmatched.Rdata
# -- voter_matrix in data/voter_matrix.csv
# -- candidate_matrix in data/candidate_matrix.csv (revised)
# -- core_for_ggplot in data/core_for_ggplot_rep_global.csv
# -- core_for_ggplot in data/core_for_ggplot_dem_global.csv (for dems)
# -- core_for_ggplot in data/core_for_ggplot_rep_global.csv (for reps)
# -- all_score,rep_score,dem_score, in data/scoreMats.Rdata
# -- data_matrix (just combining revised candidate_matrix & voter_matrix) in data_matrix_scored.Rdata
